package com.power.controller;

import com.power.domain.SysMenu;
import com.power.model.Result;
import com.power.service.SysMenuService;
import com.power.util.AuthUtils;
import com.power.vo.MenuAndAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description 系统权限控制层
 * @Author dingchunlin
 * @Date 2024/07/12 16:42
 * @Version 1.0.0
 */
@Api(tags = "系统权限接口管理")
@RequestMapping("/sys/menu")
@RestController
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询用户的菜单权限和操作权限
     * @return
     */
    @ApiOperation(value = "查询用户的菜单权限和操作权限")
    @GetMapping("/nav")
    public Result loadUserMenuAndAuth() {
        // 获取登录用户的标识
        Long loginUserId = AuthUtils.getUserId();
        // 查询用户的操作权限
        Set<String> perms = AuthUtils.getLoginUserPerms();
        // 查询用户的菜单权限
        Set<SysMenu> menus = sysMenuService.queryUserMenuListByUserId(loginUserId);
        // 返回结果
        MenuAndAuth menuAndAuth = new MenuAndAuth(menus, perms);
        return Result.success(menuAndAuth);
    }

    /**
     * 查询系统所有权限集合
     * @return
     */
    @ApiOperation(value = "查询系统所有权限集合")
    @GetMapping("/table")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    public Result<List<SysMenu>> loadAllSysMenuList() {
        List<SysMenu> sysMenuList = sysMenuService.queryAllMenuList();
        return Result.success(sysMenuList);
    }

    /**
     * 新增权限菜单
     * @param sysMenu 权限菜单
     * @return Result
     */
    @ApiOperation(value = "新增权限菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:save')")
    @PostMapping
    public Result<String> saveSysMenu(@RequestBody SysMenu sysMenu) {
        Boolean save =sysMenuService.saveSysMenu(sysMenu);
        return Result.handle(save);
    }

    /**
     * 根据id查询菜单权限
     * @param menuId 菜单id
     * @return Result
     */
    @ApiOperation(value = "根据id查询菜单权限")
    @PreAuthorize("hasAnyAuthority('sys:menu:info')")
    @GetMapping("/info/{menuId}")
    public Result<SysMenu> loadSysMenuInfo(@PathVariable("menuId") Long menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return Result.success(sysMenu);
    }

    /**
     * 修改权限菜单信息
     * @param sysMenu 权限菜单对象
     * @return Result
     */
    @ApiOperation(value = "修改权限菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:update')")
    @PutMapping
    public Result<String> modifySysMenu(@RequestBody SysMenu sysMenu) {
        Boolean modify = sysMenuService.modifySysMenu(sysMenu);
        return Result.handle(modify);
    }

    /**
     * 删除权限菜单
     * @param menuId 菜单id
     * @return Result
     */
    @ApiOperation(value = "删除权限菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:delete')")
    @DeleteMapping("/{menuId}")
    public Result<String> removeSysMenu(@PathVariable("menuId") Long menuId) {
        Boolean remove = sysMenuService.removeSysMenuById(menuId);
        return Result.handle(remove);
    }


}
